$(function(){
    var $register_username;
    var $register_password;
    var $register_password_confirm;
    var $reg1 = /^[A-Za-z0-9]{3,11}$/;//用户名要求是3~11的位的字母或者数字
    var $reg2 = /^[A-Za-z0-9]{6,11}$/;//密码要求是6~11的位的字母或者数字

    //注册事件
    $('#box_up_r').on('click', function() {
        $.ajax({
            "type":"GET",
            "url":".....action",
            "dataType":"json",
            "contentType":"charset=UTF-8",
            "data":{"username":$register_username,"password":$register_password},
            "beforeSend":function(){
                if($('.error').text()!=""||$register_username==""||$register_password==""||$register_password_confirm==""){
                    return false;
                }
            },
            "success":function(data){
                var data = $.parseJSON(data.content);
                console.log(data[0].username);
                $('#box_up_r').before('<p class="login-success">注册成功！</p>');
                setTimeout(function(){
                    location.reload();
                },500)
            },
            "error":function(){
                alert("网络不畅，请稍后再试！");
            }
        })
    });

    //检测用户名是否被占用和是否符合规范
    $('#register_username').on('blur',  function(event) {
        event.preventDefault();
        var $username = $(this).val();
        if($username==""){
            $('.error-username').text('用户名不能为空');
        }
        else if(!$reg1.test($username)) {
            $('.error-username').text('用户名不合规范');
        }
        else{
            $register_username = encodeURI($username);
            $('.error-username').text('');
            $.ajax({
                "url": '/Hotel/check.action',
                "type": 'GET',
                "dataType": 'json',
                "data": {"username": $register_username},
                "success":function(data){
                    var data = $.parseJSON(data.content);
                    if(data[0].status=="0"){
                        $('.error-username').text('用户名已被占用');
                    }
                    else if(data[0].status=="1"){
                        $('.error-username').text('');
                    }
                }
            })
        }
    });

    //检测密码是否符合规范
    $('#register_password').on('blur', function(event) {
        event.preventDefault();
        var $password = $(this).val();
        if($password==""){
            $('.error-password').text('密码不能为空');
        }
        else if(!$reg2.test($password)){
            $('.error-password').text('密码不合规范');
        }
        else{
            $('.error-password').text('');
            $register_password = $password;
        }
    });

    //检测两次密码输入是否一致
    $('#register_password_confirm').on('blur', function(event) {
        event.preventDefault();
        var $password = $(this).val();
        if($register_password_confirm!=$register_password){
            $('.error-password-confirm').text('两个密码不一致');
        }
        else{
            $('.error-password-confirm').text('');
            $register_password_confirm = $password;
        }
    });